

package com.maoshi.shop.security.api.filter;

import com.maoshi.shop.common.exception.MaoshiShopBindException;
import com.maoshi.shop.security.api.model.MaoshiUser;
import com.maoshi.shop.security.api.service.AppLoginService;
import com.maoshi.shop.security.comment.filter.MaoshiAuthenticationProcessingFilter;
import com.maoshi.shop.security.comment.token.AuthenticationToken;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Objects;

/**
 * 自定义登陆filter
 * 统一第三方登陆
 * @author LGH
 */
@Component
public class AppLoginAuthenticationFilter extends MaoshiAuthenticationProcessingFilter {

    private final List<AppLoginService> appLoginServices;

    @Autowired
    public AppLoginAuthenticationFilter(List<AppLoginService> appLoginServices) {
        super("/appLogin");
        this.appLoginServices = appLoginServices;
    }

    @Override
    protected UserDetails getUserDetails(AuthenticationToken authentication) {
        for (AppLoginService appLoginService : appLoginServices) {
            // 判断登陆方式
            if (Objects.equals(appLoginService.getApp().value(),authentication.getAppType())) {
                return appLoginService.loginAndGetUserDetails(authentication);
            }
        }
        throw new MaoshiShopBindException("无法获取登陆方式");
    }


}
